/**
 * related to typeSelect.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-02
 */
var page = sm("do_Page");
var app = sm("do_App");

var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");

var listviewlef = ui("do_ListView_1");
var listviewlef2 = ui("do_ListView_2");
var leftdata = mm("do_ListData");
listviewlef.bindItems(leftdata);
var leftdata2 = mm("do_ListData");
listviewlef2.bindItems(leftdata2);

var firstTypeId = '',typeId = '';
var jsonData = {};

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(300,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:460};
animPanelShow.append(300,propsPS,"Linear");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(300,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(300,propsPH,"EaseIn");

page.on("types",function(data){
	maskbg.visible = data;
	bodyp.animate(animPanelShow);
	maskbg.animate(animMaskShow);
}).on("loaded",function() {
	category();
});

//遮罩事件
var maskbg = ui("bgmask");
maskbg.on("touch","",300,function(){
	bodyp.animate(animPanelHide);
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
});
//防止穿透
var bodyp = ui("pbody");
bodyp.on("touch","",300,function(){
	
});

function category(){
	qqHttp.post(homemakingUtil.url.type.findParent, {}, function(data) {
		leftdata.addData(data.parentList);
		listviewlef.refreshItems();
	})
};

function secondCategory(pid){
	qqHttp.post(homemakingUtil.url.type.findChildren, {parentId:pid}, function(data) {
		leftdata2.removeAll();
		leftdata2.addData(data.childrenList);
		listviewlef2.refreshItems();
	})
}

//var btn = ui("do_Button_pane1");
listviewlef.on("touch",function(index){
	var _typeId = leftdata.getOne(index).id;
	jsonData.firstTypeId = _typeId;
	jsonData.firstTypeName = leftdata.getOne(index).name;
	if(firstTypeId!=_typeId){
		firstTypeId = _typeId;
		firstTypeId= firstTypeId==undefined? "":firstTypeId;
		//var btnTitle = leftdata.getOne(index).name;
		//btn.text = btnTitle=="全部分类"? "全部分类":btnTitle;
		for(var i=0;i<leftdata.getCount();i++){
			leftdata.updateOne(i, {
				"name":leftdata.getOne(i).name,
				"id":leftdata.getOne(i).id,
				"bgcolor":i==index? "f2f2f2ff":"f2f2f200"
			});
		}
		listviewlef.refreshItems();
		secondCategory(_typeId);
	}
});

//var btn = ui("do_Button_pane1");
listviewlef2.on("touch",function(index){
	var _typeId = leftdata2.getOne(index).id;
	jsonData.typeId = _typeId;
	jsonData.typeName = leftdata2.getOne(index).name;
	if(typeId!=_typeId){
		typeId = _typeId;
		typeId= typeId==undefined? "":typeId;
		//var btnTitle = leftdata2.getOne(index).name;
		//closepanel(true);
		//btn.text = btnTitle=="全部分类"? "全部分类":btnTitle;
		for(var i=0;i<leftdata2.getCount();i++){
			leftdata2.updateOne(i, {
				"name":leftdata2.getOne(i).name,
				"id":leftdata2.getOne(i).id,
				"bgcolor":i==index? "f2f2f2ff":"f2f2f200"
			});
		}
		listviewlef2.refreshItems();
	}
	page.fire("refreshType",jsonData);
	bodyp.animate(animPanelHide);
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
});